This is the model for the lower reach of a small, turbulent, shaded stream with a fair amount of fine particulate organic matter, Glenbrook Creek NV, USA.
We then ran normal pooling on k600 in stream metabolizer model:
b_Kb_oipi_tr_plrckm.stan to get modeled K600 to see if we
could resolve the negative correlation between ER and K600. Priors on
K600_lnQ_nodes_meanlog were set as 5 bins based on
rnorm(1000, mean = logQ_mean, sd = logQ_sd) centered around
the mean and logQ values 1-2 sd away from the mean.
We chose segments of time where we believe GPP occurred and was greater than 0. These chunks of time are from a previous model where we binned flow and incorporated measured and estimated K600 priors from gas exchange measurements a the reach.
This the raw model output. It looks like there are many days with fitting issues. Where GPP is in blue and ER is in orange.
Here is the run configuration for full model:
mm_name(type = 'bayes', pool_K600 = "binned", err_obs_iid = TRUE, err_proc_iid = TRUE, ode_method = "trapezoid", deficit_src = 'DO_mod', engine = 'stan')
Fitting priors:
K600_lnQ_nodes_meanlog = log(25) Where 25 was the mean
value from observed measurements and normal pooled modeled,
K600_lnQ_nodes_sdlog = 1.1 was 1.1,
bayes_specs_new$K600_lnQ_nodes_centers <- log_bins was
from
prior_samples <- rnorm(1000, mean = logQ_mean, sd = logQ_sd)
based on mean discharge for all flows > 0.99 cms to cut off some
extreme values.
Make sure the chains converged; all r-hat values were well below 1.05 (the red line) for GPP, ER, and K600. The blue lines are the mean for each parameter.
## [1] 1.013708
## [1] 1.001413
## [1] 1.012106
# Create a dataframe for the vertical lines
rhat_lines <- data.frame(
Param = c("K600_daily_Rhat", "GPP_daily_Rhat", "ER_daily_Rhat"),
yintercept = c(k600.rhat, GPP.rhat, ER.rhat)
)
met.full %>%
select(date, GPP_daily_Rhat, ER_daily_Rhat, K600_daily_Rhat) %>%
pivot_longer(2:4, values_to = "Rhat", names_to = "Param") %>%
ggplot(aes(x = date, y = Rhat), col=lab) +
geom_hline(yintercept = 1.05, col = "red", lty = 2) +
geom_point(size = 0.5, alpha = 0.5) +
geom_hline(data = rhat_lines, aes(yintercept = yintercept), col = "blue", lty = 2) +
theme_classic() +
facet_wrap(~Param, ncol = 3)In general convergence looks alright, but there are still many days with unrealistic GPP or ER.
## date lab rmse sd
## Min. :2021-03-13 Length:1912 Min. :0.0088 Min. :0.004922
## 1st Qu.:2022-02-17 Class :character 1st Qu.:0.0364 1st Qu.:0.139621
## Median :2023-04-17 Mode :character Median :0.0550 Median :0.235482
## Mean :2023-01-30 Mean :0.1008 Mean :0.276757
## 3rd Qu.:2024-01-24 3rd Qu.:0.1094 3rd Qu.:0.367734
## Max. :2024-10-08 Max. :0.9391 Max. :1.070590
## NA's :506
## min max range nrmse
## Min. : 3.755 Min. : 5.535 Min. :0.01333 Min. :0.0133
## 1st Qu.: 6.796 1st Qu.: 7.567 1st Qu.:0.46592 1st Qu.:0.0466
## Median : 7.747 Median : 8.687 Median :0.73883 Median :0.0769
## Mean : 7.702 Mean : 8.578 Mean :0.87562 Mean :0.0935
## 3rd Qu.: 9.064 3rd Qu.: 9.717 3rd Qu.:1.15217 3rd Qu.:0.1274
## Max. :10.061 Max. :11.395 Max. :3.37000 Max. :0.3636
## NA's :506
## minT maxT rangeT minQ
## Min. : 1.000 Min. : 1.015 Min. :0.007333 Min. :0.0006898
## 1st Qu.: 1.808 1st Qu.: 3.979 1st Qu.:1.653750 1st Qu.:0.0071279
## Median : 6.000 Median : 9.627 Median :2.533833 Median :0.0167851
## Mean : 5.876 Mean : 8.688 Mean :2.811335 Mean :0.0424491
## 3rd Qu.: 9.369 3rd Qu.:12.758 3rd Qu.:3.742500 3rd Qu.:0.0338001
## Max. :13.870 Max. :16.380 Max. :7.573333 Max. :0.6346135
##
## maxQ rangeQ
## Min. :0.0009197 Min. :0.000000
## 1st Qu.:0.0108068 1st Qu.:0.002529
## Median :0.0243728 Median :0.005978
## Mean :0.0609296 Mean :0.018481
## 3rd Qu.:0.0506618 3rd Qu.:0.016613
## Max. :0.7764051 Max. :0.393798
##
Here is the run configuration to get modeled K600:
mm_name(type = 'bayes', pool_K600 = "normal", err_obs_iid = TRUE, err_proc_iid = TRUE, ode_method = "trapezoid", deficit_src = 'DO_mod', engine = 'stan')
Where each “lab” segment was run as individual model with the
K600_lnQ_nodes_meanlog adjusted to match streamline in cms
during that time.
There is a strong negative correlation between ER and K600 (-0.933). GPP and K600 are slightly less correlated (-0.074). Still seeing a strange/paradoxical negative correlation between flow and K600.
## [1] 15.3487
## [1] 39.53105
## [1] 27.43988
KER_cor <- round(cor(met.df$ER_daily_mean, met.df$K600_daily_mean, use = "complete.obs"),3)
print(KER_cor)## [1] -0.933
KGPP_cor <-round(cor(met.df$GPP_daily_mean, met.df$K600_daily_mean, use = "complete.obs"),3)
print(KGPP_cor)## [1] -0.074
The vertical dashed is the overall mean modeled K600 in the box plot.
Plots for (1) measured v modeled K600 and flow and (2) logK600 and log(flow+1).
Could be one poor measurement at the highest flow for measured gas
exchange. But in general the modeled K600 does seem similar to the
measured, which is kind of nice to see how robust the
pool_K600 = "normal" is getting at K600.
Here is the run configuration for full model:
mm_name(type = 'bayes', pool_K600 = "binned", err_obs_iid = TRUE, err_proc_iid = TRUE, ode_method = "trapezoid", deficit_src = 'DO_mod', engine = 'stan')
Where dashed vertical lines correspond to the prior locations for
flow bins in
bayes_specs_new$K600_lnQ_nodes_centers <- log_bins
dat1 <- input_dat%>% filter(discharge<0.99)
## Set bayes specs
bayes_name_new <- mm_name(type = 'bayes', pool_K600 = "binned",
err_obs_iid = TRUE, err_proc_iid = TRUE,
ode_method = "trapezoid", deficit_src = 'DO_mod', engine = 'stan')
bayes_specs_new <- specs(bayes_name_new)
# Compute log-transformed discharge
logQ <- log(na.omit(dat1$discharge))
sd_logQ <- round(sd(na.omit(logQ)),2)
## Compute log-transformed K600 for prior estimation
logQ_mean <- round(mean(logQ, na.rm = TRUE),2) # Center the prior flow mean
logQ_mean## [1] -3.96
## [1] 1.3
## [1] 3.2
## [1] 1.1
## [1] 73.69979
## [1] 8.16617
prior_samples <- rnorm(1000, mean = logQ_mean, sd = logQ_sd)
# Define bins based on log-scale mean and SD
log_bins <- c(logQ_mean - c(2 * logQ_sd),
logQ_mean - c(1 * logQ_sd),
logQ_mean,
logQ_mean + c(1 * logQ_sd),
logQ_mean + c(2 * logQ_sd))
# Plot:
#bins <- exp(log_bins)
prior_df <- data.frame(Q_cms = prior_samples)
prior_bins_plot <- ggplot(prior_df, aes(x = Q_cms)) +
geom_density(fill = "lightblue", alpha = 0.5) + # Density plot
geom_vline(xintercept = log_bins, color = "black", lty="dashed") + # Bin edges
labs(title = "Binned log(flow) for K600 Priors", y = "Density", x = "log(Q) (cms)") +
theme_bw()
prior_bins_plot# Assign priors for binned K600
bayes_specs_new$K600_lnQ_nodes_meanlog <- rep(logK600_mean, length(log_bins)) # Centered at 25
bayes_specs_new$K600_lnQ_nodes_meanlog## [1] 3.2 3.2 3.2 3.2 3.2
bayes_specs_new$K600_lnQ_nodes_sdlog <- rep(logK600_sd, length(log_bins)) # Wide enough to allow 40
bayes_specs_new$K600_lnQ_nodes_sdlog ## [1] 1.1 1.1 1.1 1.1 1.1
## [1] -6.56 -5.26 -3.96 -2.66 -1.36
# Keep other parameters
bayes_specs_new$K600_daily_sigma_sigma <- 0.05
bayes_specs_new$n_chains <- c(3)
bayes_specs_new$n_cores <- c(3)
bayes_specs_new$burnin_steps <- c(2500)
bayes_specs_new$saved_steps <- c(2500)
bayes_specs_new## Model specifications:
## model_name b_Kb_oipi_tr_plrckm.stan
## engine stan
## split_dates FALSE
## keep_mcmcs TRUE
## keep_mcmc_data TRUE
## day_start 4
## day_end 28
## day_tests full_day, even_timesteps, complete_data, pos_disch...
## required_timestep NA
## K600_lnQ_nodes_centers -6.56, -5.26, -3.96, -2.66, -1.36
## GPP_daily_mu 3.1
## GPP_daily_lower -Inf
## GPP_daily_sigma 6
## ER_daily_mu -7.1
## ER_daily_upper Inf
## ER_daily_sigma 7.1
## K600_lnQ_nodediffs_sdlog 0.5
## K600_lnQ_nodes_meanlog 3.2, 3.2, 3.2, 3.2, 3.2
## K600_lnQ_nodes_sdlog 1.1, 1.1, 1.1, 1.1, 1.1
## K600_daily_sigma_sigma 0.05
## err_obs_iid_sigma_scale 0.03
## err_proc_iid_sigma_scale 5
## params_in GPP_daily_mu, GPP_daily_lower, GPP_daily_sigma, ER...
## params_out GPP, ER, DO_R2, GPP_daily, ER_daily, K600_daily, K...
## n_chains 3
## n_cores 3
## burnin_steps 2500
## saved_steps 2500
## thin_steps 1
## verbose FALSE
Plots made on filtered data: met.clean
filtered for days with
GPP_daily_Rhat<1.05,ER_daily_Rhat<1.05,
K600_daily_Rhat <1.05, as well as
(GPP_97.5pct>0) and (ER_2.5pct<0).
met.clean <- met.full %>%
filter(GPP_daily_Rhat<1.05)%>%
filter(GPP_97.5pct>0)%>%
filter(ER_daily_Rhat<1.05) %>%
filter(ER_2.5pct<0)%>%
filter(K600_daily_Rhat<1.05)
mean_k_mod <- mean(met.clean$K600_daily_mean)
mean_k_mod## [1] 16.93494
## [1] 39.53105
KER_cor <- round(cor(met.clean$ER_daily_mean, met.clean$K600_daily_mean, use = "complete.obs"),3)
print(KER_cor)## [1] -0.759
KGPP_cor <-round(cor(met.clean$GPP_daily_mean, met.clean$K600_daily_mean, use = "complete.obs"),3)
print(KGPP_cor)## [1] 0.073
The vertical dashed is the overall mean modeled K600 in the box plot.
The direction of the K600 ~ flow relationship looks more logical, where K600 increases with flow. ER and K600 are still very negatively correlated (-0.759) but it might just mean that ER is not an identifiable parameter in this reach.
The mean modeled K600 is lower than expected, 16.9 when it should be closer to 25. Overall this could just be a challenging reach to fit the model to.
Where GPP is in blue and ER is in orange, and the black points represent NEP.
Of the 1037 days with DO observations 699 days were removed.
| Number of Days | Explaination | Percent of Days |
|---|---|---|
| 1037 | Total days of DO observations | 100.0 |
| 699 | Total days removed | 67.4 |
| 334 | Days model was unable to fit | 32.2 |
| 0 | days where GPP rhat > 1.05 | 0.0 |
| 0 | days where ER rhat > 1.05 | 0.0 |
| 0 | days where K600 rhat > 1.05 | 0.0 |
| 365 | days where modeled GPP was negative | 35.2 |
| 5 | days where modeled ER was positive | 0.5 |